﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.ApplicationServer.Caching;
using AppFabricSample.Data;
using System.Reflection;

namespace AppFabricSample.CacheProvider
{
    public class CacheProvider : DataCacheStoreProvider
    {

        public static String dataCacheName;
        //private String connectionString;
        public static Dictionary<string, string> configurationsDetails;

        public CacheProvider(string cacheName, Dictionary<string, string> config)
        {
            dataCacheName = cacheName; //Store the cache name for future use
            configurationsDetails = config;
            //connectionString = config[0].Value;
        }
        
        protected override void Dispose(bool disposing)
        {
            throw new NotImplementedException();
        }

        public override void Read(System.Collections.ObjectModel.ReadOnlyCollection<DataCacheItemKey> keys, IDictionary<DataCacheItemKey, DataCacheItem> items)
        {
            foreach (var key in keys)
            {
                items[key] = Read(key);
            }
        }

        public override DataCacheItem Read(DataCacheItemKey key)
        {
            
            DataCacheItem cacheItem;

            //String ClassName = CacheKeyFunctionMapping.GetClassName(key.Key);
            //String FunctionName = CacheKeyFunctionMapping.GetFunctionName(key.Key);
            //Object[] FuntionParameters = CacheKeyFunctionMapping.GetParameters(key.Key);


            String ClassName = CacheKeyFunctionMapping.GetClassName(key.Key);
            String FunctionName = CacheKeyFunctionMapping.GetFunctionName(key.Key);


            var ClassType = Type.GetType(ClassName);
            MethodInfo ClassFunctionMethod = ClassType.GetMethod(FunctionName,
                BindingFlags.Static | BindingFlags.Public | BindingFlags.InvokeMethod | BindingFlags.FlattenHierarchy);

            Object[] FuntionParameters = CacheKeyFunctionMapping.GetParameters(key.Key, ClassFunctionMethod);


            var retrievedValue = ClassFunctionMethod.Invoke(null, FuntionParameters);




            if (retrievedValue == null)
            {
                cacheItem = null;
                Console.WriteLine("Not Cached");    
            }
            else
            {
                cacheItem = DataCacheItemFactory.GetCacheItem(key, dataCacheName, retrievedValue, null);
                Console.WriteLine("Cached");    
            }
            return cacheItem; 
        }

        //private Student ReadFromStudentDatabase(string p)
        //{
        //    Console.WriteLine("Read From Database");
        //    AppFabricSample.Data.Student st = new Student();
        //    return st.GetStudent(p);
        //}

        public override void Write(IDictionary<DataCacheItemKey, DataCacheItem> items)
        {
            foreach (var item in items)
            {  
                Write(item.Value);
            }
               items.Clear();
        }

        public override void Write(DataCacheItem item)
        {
            Student s1 = item.Value as Student;
            //s1.UpdateStudentMark();
        }

        public override void Delete(System.Collections.ObjectModel.Collection<DataCacheItemKey> keys)
        {
            throw new NotImplementedException();
        }

        public override void Delete(DataCacheItemKey key)
        {
            throw new NotImplementedException();
        }


        
    }
}
